package logic

import (
	"context"
	"encoding/json"
	"pro/cache"
	"pro/models/mysql"
	"time"

	"pro/prorpc/internal/svc"
	"pro/prorpc/prorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DoctorListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDoctorListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DoctorListLogic {
	return &DoctorListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DoctorListLogic) DoctorList(in *prorpc.DoctorListRequest) (*prorpc.DoctorListResponse, error) {
	// todo: add your logic here and delete this line
	var doctors []mysql.Doctor

	result, _ := cache.Rdb.Get(l.ctx, "doctorList").Result()
	if result == "" {
		mysql.DB.Debug().Find(&doctors)
		marshal, _ := json.Marshal(doctors)
		cache.Rdb.Set(l.ctx, "doctorList", marshal, time.Minute*30)
	} else {
		json.Unmarshal([]byte(result), &doctors)
	}

	var doctorList []*prorpc.DoctorList

	for _, doctor := range doctors {
		list := prorpc.DoctorList{
			DoctorId:  int64(doctor.ID),
			HeadImage: doctor.HeadImage,
			Name:      doctor.Name,
			Tel:       doctor.Tel,
			Ke:        doctor.Ke,
			Price:     float32(doctor.Price),
		}
		doctorList = append(doctorList, &list)
	}

	return &prorpc.DoctorListResponse{
		List: nil,
	}, nil
}
